package com.yuju.order.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.exception.BusinessException;
import com.yuju.group.model.GuestInfoModel;
import com.yuju.group.service.IGuestInfoService;
import com.yuju.order.dao.IRoomOrderGuestDao;
import com.yuju.order.dao.entity.RoomOrderGuest;
import com.yuju.order.enums.RoomOrderGuestReturnCodeEnum;
import com.yuju.order.enums.RoomOrderReturnCodeEnum;
import com.yuju.order.model.RoomOrderGuestModel;
import com.yuju.order.model.RoomOrderModel;
import com.yuju.order.service.IRoomOrderGuestService;
import com.yuju.order.service.IRoomOrderService;
import com.yuju.util.Utils;
/**
 * 
 * 类名：RoomOrderGuestServiceImpl  <br />
 *
 * 功能：
 *
 * @author : sunyl <br />
 * @Date : 2017年11月24日 上午9:37:41  <br />
 * @version : 1.1 <br />
 */
@Service
public class RoomOrderGuestServiceImpl extends AbstractBaseService<RoomOrderGuestModel,RoomOrderGuest,String> implements IRoomOrderGuestService{
	@Autowired
	private IRoomOrderGuestDao RoomOrderGuestDao;
	@Autowired
	private IGuestInfoService guestInfoService;
	@Autowired
	private IRoomOrderService roomOrderService;
	
	@Override
	public IBaseDao<RoomOrderGuestModel, RoomOrderGuest, String> getDao() {
		// TODO Auto-generated method stub
		return RoomOrderGuestDao;
	}
	@Override
	public int save(RoomOrderGuestModel model) {
		// 保存数据
		model.setId(Utils.getUUID());
		model.setGmtCreate(new Date());
		model.setGmtModified(new Date());
		int rows = super.save((model));
		return rows;
	}
	@Override
	public int saveExecptId(RoomOrderGuestModel model) {
		// 保存数据
		model.setGmtCreate(new Date());
		model.setGmtModified(new Date());
		int rows = super.save((model));
		return rows;
	}
	@Override
	public List<RoomOrderGuestModel> listPure(RoomOrderGuestModel model) {
		RoomOrderModel roomOrderModel = roomOrderService.findById(model.getRoomOrderId());
		List<RoomOrderGuestModel> find = super.list(model);
		List<RoomOrderGuestModel> list = new ArrayList<>();
		for(RoomOrderGuestModel item : find){
			if(item.getRoomGuestId().equals(roomOrderModel.getMainGuestId())){
				continue;
			}
			list.add(item);
		}
		return  list;
		
	}
	/**
	 * 修改余额
	 * 功能: <br/>
	 * 
	 * 重写：sunyl <br/>
	 * 
	 * @Date ：2017年12月8日 下午2:18:54<br/>
	 * 
	 * @param roomOrdeGuestrId
	 * @param money
	 * @return <br/>
	 * @see com.yuju.group.service.IRoomOrderGuestService#updateRoomOrderForBalance(java.lang.String, java.math.BigDecimal)
	 */
	@Override
	public int updateRoomOrderForBalance(String roomOrdeGuestrId, BigDecimal money) {
		int rows = 0;
		RoomOrderGuestModel model = this.findById(roomOrdeGuestrId);
		model.setId(roomOrdeGuestrId);
		model.setBalance(model.getBalance().add(money));
		rows = this.updateSelective(model);
		return rows;
	}
	/**
	 * 获取同来宾客的列表信息
	 * 功能: <br/>
	 * 
	 * 重写：xuehj <br/>
	 * 
	 * @Date ：2017年12月20日 上午10:28:19<br/>
	 * 
	 * @param id
	 * @return <br/>
	 * @see com.yuju.order.service.IRoomOrderService#getOrderRela(java.lang.String)
	 */
	@Override
	public List<GuestInfoModel> getOrderGuestInfo(String roomOrderId) {
		/*1 获取订单*/
		RoomOrderGuestModel model = new RoomOrderGuestModel();
		model.setRoomOrderId(roomOrderId);
		List<RoomOrderGuestModel> finds  = super.list(model);
		if(finds == null || finds.size()==0){
			throw new BusinessException(RoomOrderGuestReturnCodeEnum.ROOM_ORDER_GUEST_QUERY_FAILURE.getCode(),
					RoomOrderGuestReturnCodeEnum.ROOM_ORDER_GUEST_QUERY_FAILURE.getDesc());
		}
		List<GuestInfoModel> guestInfoList = new ArrayList<>();
		for(RoomOrderGuestModel roomOrderGuestModel :finds ){
			guestInfoList.add(guestInfoService.findById(roomOrderGuestModel.getRoomGuestId()));
		}
		
		return guestInfoList;
	}
}
